app.model.ABSCBN = {};

(function(){
	
	app.model.ABSCBN.getHeadlineItems = function(_args){
		var xhr = Titanium.Network.createHTTPClient();
		xhr.onload = function(e){
			try{
				var doc = xhr.responseXML.documentElement;
				var channel = doc.getElementsByTagName('channel').item(0);
				var title = channel.getElementsByTagName('title').item(0).text; //Ti.API.info('title: ' + title);
				var link = channel.getElementsByTagName('link').item(0).text; //Ti.API.info('link: ' + link);
				var description = channel.getElementsByTagName('description').item(0).text; //Ti.API.info('description: ' + description);
				var itemList = channel.getElementsByTagName('item');
				var headlineItems = [];
				for(var i=0; i<itemList.length; i++){
					var itemtitle = itemList.item(i).getElementsByTagName('title').item(0).text; //Ti.API.info('itemtitle: ' + itemtitle);
					var itemdescription = itemList.item(i).getElementsByTagName('description').item(0).text; //Ti.API.info('itemdescription: ' + itemdescription);
					var itemlink = itemList.item(i).getElementsByTagName('link').item(0).text; //Ti.API.info('itemlink: ' + itemlink);
					var itempubDate = itemList.item(i).getElementsByTagName('pubDate').item(0).text; //Ti.API.info('itempubDate: ' + itempubDate);
					headlineItems.push({
						title : itemtitle,
						description : itemdescription,
						link : itemlink,
						pubDate : itempubDate,
					});
				}
				if(_args.successCallback){
					_args.successCallback({
						items : headlineItems,
					});
				}
			}catch(E) {
				Ti.API.info('XML Parse Error: ' + E);
			}
		
		};
		xhr.open('GET', 'http://www.abs-cbnnews.com/rss.xml');
		xhr.send();
	}
})()
